package jp.sourceforge.akjrcp.text_douga.effect;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.VideoSystem;

public class FrameImageEffect extends FillImageEffect{

	
	public String toString(){
		String out="FrameImageEffect: "+super.toString();
			return out;
	}
	
	public FrameImageEffect(boolean effectBefore,double effectTime){
		fullPaintTime=0;
		if(effectBefore){
			setBeforeTime(effectTime);
			setAfterTime(0);
		}else{
			setBeforeTime(0);
			setAfterTime(effectTime);
		}
	}
	
	public void loadImage(int frame) throws IOException{
		if(image==null){
			if(frame<0){
				log.warn("keyframe is under 0");
				return;
			}
			String imgPath=VideoSystem.getFrameFilePath(getExportDir(), frame);
			File file=new File(imgPath);
			if(file.exists()){
				image=ImageIO.read(file);
			}
		}
	}
	

	public void effectKeyFrame(DougaPreference preference, BufferedImage baseImage, int frame, double time) throws IOException {
		if(getAfterTime()>0){
			loadImage(getKeyFrame()-1);//fill all
			super.effectAfter(preference, baseImage, frame, time);
		}
		//super.effectBefore(preference, baseImage, frame, time);
	}
	
	@Override
	public void effectBefore(DougaPreference preference, BufferedImage baseImage, int frame,double time) throws IOException {
		loadImage(getKeyFrame());
		super.effectBefore(preference, baseImage, frame, time);
	}
	
	@Override
	public void effectAfter(DougaPreference preference, BufferedImage baseImage, int frame,double time) throws IOException {
		loadImage(getKeyFrame()-1);
		super.effectAfter(preference, baseImage, frame, time);
	}
}
